﻿#include <iostream>
using namespace std;
/**************************************
工厂模式主要接解决使用new 实现细节产生的依赖导致问题（依赖具体类）
同时也具有在编译时绑定 改成运行时绑定的特性实现了在实现细节上的松耦合
**************************************/


class  Phone { //操作 基类
    virtual void make() =0;
    virtual ~Phone(){

    }
};

class MiPhone :public Phone {
    public:
     MiPhone() {
        make();
    }

    void make() override{
        cout<<("make xiaomi phone!")<<endl;;
    }
};
class IPhone :public Phone {
public:
    IPhone() {
        make();
    }
    void make()override {
        cout<<("make iphone!")<<endl;;
    }
};
class Factory {

    virtual Phone *makePhone(string phoneType) = 0;
}
class PhoneFactory :public Factory{
    public:
    Phone *makePhone(string phoneType) {
        if(phoneType.find("MiPhone")){
            return new MiPhone();
        }
        else if(phoneType.find("iPhone")) {
            return new IPhone();
        }
        return nullptr;
    }
};
int main(void)
{
    Factory *factory = new PhoneFactory();
    Phone *miPhone = factory.makePhone("MiPhone");            // make xiaomi phone!
    Phone *iPhone = factory.makePhone("iPhone");    // make iphone!
    delete factory;
    delete miPhone;
    delete iPhone;
    return 0;
}
